import { createApp } from 'vue'
import './style.css'
import 'virtual:uno.css'
import App from './App.vue'
import router from './router'
import { globalRegisterComponents } from './components'
import { globalRegisterDirectives } from './directives'
import { guard } from './router/guards'
import { loadingRouter } from './router/routeInit'
import { createPinia } from 'pinia'

const app = createApp(App)
const pinia = createPinia()

async function globalInit() {
    // 注册指令
    globalRegisterDirectives(app)

    // 注册全局组件
    await globalRegisterComponents(app)

    /**
     * 首次进入页面 如果已经有登录 在挂载路由之前加载动态路由，就不会警告找不到路由了 [Vue Router warn]: No match found
     * 即便警告也没关系 也会照常加载动态路由
     * */
    const token = localStorage.getItem('token')
    if (token) {
        await loadingRouter()
    }

    // 挂载路由
    app.use(router)
    app.use(pinia)

    // 开启路由拦截
    guard()

    // 挂载应用
    app.mount('#app')
}

globalInit()
